//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace ViNoToolkit{

	/// <summary>
	/// KAG interpreter.
	/// </summary>
	public class KAGInterpreter : ScriptInterpreter{
		
		// ----------- Message Tag ----------------.
		
		public override int HideMessage( List<byte> byteList ){
			CodeGenerator.AddHideMessageCode( byteList , KAGTagParser.m_CurrentMessageTarget );
			return ParseUntilCloseTag();
		}	

		public override int WaitTouch( List<byte> byteList ){
			// Set Default Message Target to "message0".
			CodeGenerator.AddCurrentCode( byteList , KAGTagParser.m_CurrentMessageTarget );
			string str = m_StrBuild.ToString();
			if( str.Contains( "$" ) ){
				ScriptInterpreter.AnalyzeVar( byteList , str );
			}
			else{
				ByteCodeScriptTools.AddStringCode( byteList , str );		
			}			
			byteList.Add( Opcode.PRINT );
			byteList.Add( Opcode.WAIT_TOUCH );
	/*
	// TODO !
	#if UNITY_EDITOR
			if( ! Application.isPlaying ){
				SingleDialog dialog = KAGInterpreter.m_CurrentNode.AddComponent<SingleDialog>();
				dialog.data = new DialogPartData();
				dialog.data.dialogText = m_StrBuild.ToString();
			}
	#endif
	//*/
			m_StrBuild = new System.Text.StringBuilder();		
			return ParseUntilCloseTag();
		}

		public override int EndLinkTag( List<byte> byteList ){
			string target = "start";
			if( m_AttrTable.ContainsKey( "target" ) ){
				target = m_AttrTable[ "target" ] as string;
				target = target.Replace( "*" , "" );
			}

			if( m_AttrTable.ContainsKey( "storage" ) ){
				// TODO. Load another Scenario.
			}

			// Remove Line Break.
			string text = m_StrBuild.ToString();
			text = text.Replace( "\n" , "" );

			m_StrBuild = new System.Text.StringBuilder();
			CodeGenerator.GenerateASelection( byteList , KAGTagParser.m_SelectionIndex , target , text , false , "TODO..." );	
			KAGTagParser.m_SelectionIndex++;

	#if UNITY_EDITOR
			if( ! Application.isPlaying ){
				KAGInterpreter.m_CurrentNode.AddComponent<SelectionsNode>();
//				lsn.anyResourcePath = resourcePath;
			}
	#endif	
			return ParseUntilCloseTag();
		}


	}

}
